<?php

/**
 * Created by PhpStorm.
 * User: yuqing
 * Date: 15-10-7
 * Time: 上午9:26
 */
class Controller_checkattr extends Template
{
    /**
     *生成质检单，不合格处理的功能,且生成入库单
     */
    public function before()
    {
        if(!ACL::check('checkattr')) {
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect(URL::base(TRUE));
        }
        parent::before();
    }

    //扫描属性质检
    public function Action_index()
    {
        $store              = array();
        $this->title        = "属性确认";
        $warehouse_id       = Kohana::$config->get('warehouse')->warehouse_id;
        $warehouse_location = Kohana::$config->get('warehouse')->warehouse_location;
        if(isset($_GET['orderid'])){
            $orderid = trim($_GET['orderid']);
        }else{
            $orderid = '';
        }
        $remark = '';
        $stores = Kohana::$config->load('vmi_warehouse')->as_array();
        if($orderid){

            $query = DB::select()
                ->from('recieve_invoices')
                ->join('recieve_details')
                ->on('recieve_invoices.id','=','recieve_details.recieve_invoice_id')
                ->where('recieve_details.id','=',$orderid)
                ->execute('newerp')
                ->as_array();
            //添加采购订单备注
            if(isset($query[0]['purchaseorder_id'])){
                $sql         = "select remark from purchaseorders where id=".$query[0]['purchaseorder_id'];
                $remark_temp = DB::query(Database::SELECT,$sql)
                    ->execute('newerp')
                    ->current();
                if(isset($remark_temp['remark'])){
                    $remark = $remark_temp['remark'];
                }

                $store['storeName'] = '';
                if(isset($stores[$query[0]['store']])){
                    if($query[0]['store']==0){
                        $store['storeName'] = $warehouse_location;
                    }else{
                        $store['storeName'] = $stores[$query[0]['store']];
                    }
                }else{
                    $store['storeName'] = '未知仓库';
                }

                $store['storeId'] = $query[0]['store'];
                //$store['storeName'] = isset($stores[$query[0]['store']]) ? $stores[$query[0]['store']] : '未知仓库';
                //$store['storeName'] = $warehouse_location;
            }
            //yuqing ----begin-----
            $sql  = "SELECT `id` FROM `check_quality_details` WHERE `recieve_detail_id` =  $orderid";
            $arr  = DB::query(Database::SELECT,$sql)
                ->execute('newerp')
                ->as_array('id');
            $sql  = "SELECT `id` FROM `attr_log` WHERE `recieve_detail_id` = $orderid";
            $arr1 = DB::query(Database::SELECT,$sql)
                ->execute('newerp')
                ->as_array('id');

            $isChecked = FALSE;
            if($arr1||$arr){
                $isChecked = TRUE;
            }
            $sql = "  SELECT
                          `att_time`,`user_id`
                      FROM
                          `attr_log` al
                      WHERE
                            al.`recieve_detail_id` = $orderid ";
            $res = DB::query(Database::SELECT,$sql)
                ->execute('newerp')
                ->as_array();
            if($res){
                $checkTime = $res[0]['att_time'];
                $user_id   = $res[0]['user_id'];
                $sql       = "SELECT `nick` FROM `users` WHERE id = $user_id";
                $res       = DB::query(Database::SELECT,$sql)
                    ->execute()
                    ->as_array();
                $userName  = $res[0]['nick'];
            }
            //yuqing ----end-----

            //yuqing 添加属性信息
            $logicNames = Kohana::$config->load('logic')->get('logics');
            $logictype  = array_keys($logicNames);
            $SKU        = strtoupper($query[0]['sku']);
            $skuArr     = array($SKU);
            $logics     = Skusystemapi::getSkuArrAttr($skuArr);
            /*if(!isset($logics[$SKU]) OR empty($logics[$SKU]) OR !in_array($logics[$SKU],$logictype)){*/
            if(!$arr1){
                $showSelect = "<select name='logic' id='slt_logic'>
                                    <option value='' selected='true'>--请选择属性--</option>";
                foreach($logicNames as $key=>$value){
                    $showSelect .="<option value='".$key."'>".$key."#".$value."</option>";
                }
                $showSelect .="</select>";

            }else{
                if(isset($logics[$SKU])&&in_array($logics[$SKU],$logictype)){
                    //3.1显示该物流属性
                    $logic = sprintf('%03d#',$logics[$SKU]).$logicNames[$logics[$SKU]];
                }
            }
            //yuqing
        }else{
            $query = '';
        }
        $all_count = 0;
        if(!empty($query)){
            foreach($query as $key => $value){
                if($value['supplier']){
                    //------------------------------cwy------------------------------------------------------
                    $id       = $value['supplier'];
                    $supplier = supplierskus::getSuppliers(array('name'),array('id' => "=$id"));
                    if(!empty($supplier[0]['name'])){
                        $query[$key]['supplier'] = $supplier[0]['name'];
                    }else{
                        $query[$key]['supplier'] = '';
                    }
                }
                $all_count++;

                $select_sql = "SELECT id,check_quantity FROM check_quality_details WHERE recieve_detail_id =".$query['0']['id'];
                $arr        = DB::query(Database::SELECT,$select_sql)
                    ->execute('newerp')
                    ->as_array();
                if(!empty($arr)){
                    $query[$key]['xu_check'] = $query[$key]['arrival_quantity']-$arr['0']['check_quantity'];
                }else{
                    $query[$key]['xu_check'] = $query[$key]['arrival_quantity'];
                }
            }
        }else{

            if(count($_GET)>1){
                Message::error('找不到该收货明细！');
            }
        }

        $view = View::factory('quality/checkattr');
        $view->bind('userName',$userName);
        $view->bind('checkTime',$checkTime);
        $view->bind('isChecked',$isChecked);
        $view->bind('huo',$query);
        $view->bind('store',$store);
        $view->bind('all_count',$all_count);
        $view->bind('showSelect',$showSelect);
        $view->bind('logic',$logic);
        $view->remark = $remark;
        $this->response->body($view);
    }

    public function Action_updateAttr()
    {
        $data = array();
        $column = array('purchaseorder_id','recieve_detail_id','att_time','user_id');
        $msg    = array();
        if(!empty($_GET['logic_v'])&&$_GET['logic_v']!='undefined'){
            $logicArr = array(strtoupper($_GET['sku']) => $_GET['logic_v']);
            Skusystemapi::updateSkuArrAttr($logicArr);
            $msg['sku_logic'] = 1;
            $data = array(
                'sku'     => trim(strtoupper($_GET['sku'])),
                'logic_v' => $_GET['logic_v']
            );
            $column = array_merge(array('sku','logic_v'),$column);
        }
        //VMI预约单号
        $pu_id = $_GET['pu_id'];
        //收获明细id
        $rd_id    = $_GET['recieve_detail_id'];
        $att_time = date('Y-m-d H:i:s');
        $user_id  = User::active_user()->id;
        //        $sql           = "INSERT INTO
        //                            `attr_log` ( purchaseorder_id, recieve_detail_id, att_time, user_id)
        //                      VALUES
        //                            ('$pu_id','$rd_id','$att_time',$user_id) ";
        //        $msg['insert'] = DB::query(Database::INSERT,$sql)
        //            ->execute('newerp');
        $data = array_merge($data,array(
            'purchaseorder_id'=>$pu_id,
            'recieve_detail_id'=>$rd_id,
            'att_time'=>$att_time,
            'user_id'=>$user_id
        ));
        $msg['insert'] = DB::insert('attr_log',$column)
            ->values($data)
            ->execute('newerp');
        exit(json_encode($msg));
    }
}
